
//
//  RecordViewController.swift
//  beegin2
//
//  Created by zsy on 16/4/3.
//  Copyright © 2016年 kszz. All rights reserved.
//

import UIKit


class HomeViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource, FSCalendarDataSource, FSCalendarDelegate,StrechyScorllMoveDelegate{

    @IBOutlet var panGesture: UIPanGestureRecognizer!
    var titleOfOtherPages = ""
    
    let recordImages:[(String,String)] = [("water_back","water_ico"),
                        ("breast_back","breast_ico"),
                        ("milk_back","breast_ico"),
                        ("food_back","food_ico"),
                        ("liquid_back","liquid_ico"),
                        ("water_ico","water_ico")]
    
    let headerViewHight:CGFloat = 400
    let screenWidth =  UIScreen.mainScreen().bounds.size.width
    
    var foodItemViews:[foodItemView] = []
    
    lazy var timePickView:timePicView = {
    
        let arry:NSArray = NSBundle.mainBundle().loadNibNamed("timePickView", owner: self, options: nil)
        
        if let pickView = arry[0] as? timePicView{
            
            pickView.frame = CGRect(x: 0, y: 64, width: self.screenWidth,height:44)
            pickView.btTimeSel.addTarget(self, action: #selector(dataPickClick(_:)), forControlEvents: .TouchUpInside)
            return pickView
        }
        
        return timePicView()
    }()
    
    lazy var stagesCalendar:StageCalendarView = {
        
        var arry:NSArray = NSBundle.mainBundle().loadNibNamed("StageCalendarView", owner: self, options: nil)
        
        if let calendar = arry[0] as? StageCalendarView{
            
            calendar.frame = CGRect(x: 0, y: 108, width: self.view.frame.width, height: 220)
            
            self.view.addSubview(calendar)
            calendar.hidden = true
            calendar.calendarView.delegate = self
            calendar.calendarView.dataSource = self
            calendar.calendarView.autoAdjustTitleSize = true
            
            FSCalendar.appearance().setHeaderDateFormat("yyyy/MM")
            FSCalendar.appearance().minDissolvedAlpha = 1.0
            
            return calendar
        }
        return StageCalendarView(frame: CGRect(x: 0, y: 64, width: self.view.frame.width, height: 300))
    }()

    lazy var progressViews:[HeaderInfoView] = {
    
        var items:[HeaderInfoView] = []
        for k in 0..<3{
        
            let arry:NSArray = NSBundle.mainBundle().loadNibNamed("HeaderInfoView", owner: self, options: nil)
            
            if let progressView = arry[0] as? HeaderInfoView{
                
                progressView.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: self.headerViewHight)
                items.append(progressView)
            }

        }

        return items
        
    }()
    
    lazy var topImageView:UIImageView = {
    
        let width = UIScreen.mainScreen().bounds.size.width
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: self.headerViewHight))
        imageView.image = UIImage(named: "content_img")
        
        return imageView
    }()
    
    lazy var addRecordToolView:ToolView = {
    
        let arry:NSArray = NSBundle.mainBundle().loadNibNamed("ToolView", owner: self, options: nil)
        
        if let tool = arry[0] as? ToolView{
            
            tool.frame = CGRect(x: 0, y: 0, width: self.screenWidth, height: 44)
            tool.btBreast.addTarget(self, action: #selector(toolsButtonClick(_:)), forControlEvents: .TouchUpInside)
            tool.btMilk.addTarget(self, action: #selector(toolsButtonClick(_:)), forControlEvents: .TouchUpInside)
            tool.btFood.addTarget(self, action: #selector(toolsButtonClick(_:)), forControlEvents: .TouchUpInside)
            tool.btWater.addTarget(self, action: #selector(toolsButtonClick(_:)), forControlEvents: .TouchUpInside)
            tool.btLiquid.addTarget(self, action: #selector(toolsButtonClick(_:)), forControlEvents: .TouchUpInside)
            
            
            return tool
        }
        
        return ToolView()
    }()
    
    lazy var toolView:UIView = {
        
        let width = UIScreen.mainScreen().bounds.size.width
        let addView = UIView(frame: CGRect(x: 0, y: self.headerViewHight, width: width, height: 88))
        addView.backgroundColor = UIColor.whiteColor()
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: 34))
        label.text = "添加饮食记录"
        label.textAlignment = .Center
        label.backgroundColor = UIColor(red: 245 / 255, green: 247 / 255, blue: 250 / 255, alpha: 255 / 255)
        label.textColor = UIColor(red: 74 / 255, green: 74 / 255, blue: 74 / 255, alpha: 255 / 255)
        label.font = UIFont(name: "PingFangSC-Regular", size: 14)
        
        addView.addSubview(label)
        
        self.addRecordToolView.frame = CGRect(x: 0, y: 34, width: width, height: 44)
        addView.addSubview(self.addRecordToolView)
        
        return addView
        
    }()
    
    lazy var strechScrollView:StrechyParallaxScrollView = {
    
        let strechy = StrechyParallaxScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width , height: self.view.frame.height - 50), andTopView: self.topImageView)
        self.view.addSubview(strechy)
        
        let esView = EScrollerViewEx(frameRect: CGRect(x: 0, y: 64, width: self.view.frame.width, height: self.headerViewHight - 60), subViewsArray: self.progressViews)
        
        strechy.addSubview(esView)
        
        let tipTableView = UITableView(frame: CGRect(x: 12, y: self.headerViewHight - 82, width: self.view.frame.width - 24, height: 49), style: .Plain)
        tipTableView.backgroundColor = UIColor.clearColor()
        tipTableView.delegate = self
        tipTableView.dataSource = self
        tipTableView.separatorStyle = .None
        tipTableView.registerNib(UINib(nibName: "TipViewCell", bundle: nil), forCellReuseIdentifier: "tipViewCell")
        strechy.addSubview(tipTableView)
        
        strechy.strechyDelegate = self
        
        return strechy
    
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        initFirstView()
        
        let records = [foodRecord(type: .Water, title: "", uint: "", time: ""),
                       foodRecord(type: .Supplement, title: "", uint: "", time: ""),
                      // foodRecord(type: .liquid, title: "", uint: "", time: ""),
                       foodRecord(type: .Breast, title: "", uint: "", time: ""),
                       foodRecord(type: .Milk, title: "", uint: "", time: ""),
                       foodRecord(type: .Water, title: "", uint: "", time: ""),
                       foodRecord(type: .Supplement, title: "", uint: "", time: ""),
                       // foodRecord(type: .liquid, title: "", uint: "", time: ""),
            foodRecord(type: .Breast, title: "", uint: "", time: ""),
            foodRecord(type: .Milk, title: "", uint: "", time: ""),
            foodRecord(type: .Water, title: "", uint: "", time: ""),
            foodRecord(type: .Supplement, title: "", uint: "", time: ""),
            // foodRecord(type: .liquid, title: "", uint: "", time: ""),
            foodRecord(type: .Breast, title: "", uint: "", time: ""),
            foodRecord(type: .Milk, title: "", uint: "", time: "")]
        
        FlashRecord(records)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func initFirstView(){
    
        strechScrollView.addSubview(toolView)
        self.view.addSubview(timePickView)
    }
    
    func FlashRecord(records:[foodRecord]) {
        
        var startY = headerViewHight + 78
        
        for k in 0 ..< records.count{
        
            let arry:NSArray = NSBundle.mainBundle().loadNibNamed("foodItemView", owner: self, options: nil)
            
            if let foodView = arry[0] as? foodItemView{
                
                foodView.frame = CGRect(x: 0, y: startY + 7, width: self.view.frame.width, height: 55 )
                strechScrollView.addSubview(foodView)
                
                foodView.upLine.hidden = false
                foodView.downLine.hidden = false
                if k == 0 {
                
                    foodView.upLine.hidden = true
                    
                }else if k == records.count - 1{
                
                    foodView.downLine.hidden = true
                }
                
                foodView.backImage.image = UIImage(named: recordImages[records[k].type.rawValue].0)
                 foodView.foodIco.image = UIImage(named: recordImages[records[k].type.rawValue].1)
                
                
                strechScrollView.addSubview(foodView)
                startY += 55
            }
        }
        
        strechScrollView.setContentSize(CGSize(width: self.view.frame.width, height: startY + 10))
        
        
    }
    
    // MARK: - UITableViewDelegate
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        
        if indexPath.section == 0 && indexPath.row == 0{
            
            //self.performSegueWithIdentifier("showMilkBrandSegue", sender: self)
        }
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        return nil
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 0
    }
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let identifier = "tipViewCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier , forIndexPath: indexPath)
        cell.layer.cornerRadius = 7
        
        return cell
    }
    
    // MARK: - StrechyScorllMoveDelegate
    var hasAddToSuperView = false
    func StrechyScorllToYPos(yPos: CGFloat) {
        
        if yPos < (headerViewHight - 34){
        
            if hasAddToSuperView{
                addRecordToolView.frame = CGRect(x: 0, y: 34, width: screenWidth, height: 44)
                toolView.addSubview(self.addRecordToolView)
                hasAddToSuperView = false
                timePickView.hidden = false
            }
        }else{
            if !hasAddToSuperView{
                addRecordToolView.frame = CGRect(x: 0, y: 64, width: screenWidth, height: 44)
                self.view.addSubview(self.addRecordToolView)
                hasAddToSuperView = true
                timePickView.hidden = true
            }
        }
        
//        print(yPos)
    }
    
    func dataPickClick(sender:AnyObject){
        
        self.stagesCalendar.hidden = !self.stagesCalendar.hidden
    }
    
    // MARK: - tools button click
    func toolsButtonClick(sender:UIButton){
        
        let breastTag = 101
        let milkTag = 102
        let foodTag = 103
        let waterTag = 104
        let liquidTag = 105
        print(sender.tag)
        switch sender.tag{
            
        case breastTag:
            break
        case milkTag:
            break
        case foodTag:
            break
        case waterTag:
            break
        case liquidTag:
            break
        default: break
        }

        
        let foodChoiseVc = self.storyboard?.instantiateViewControllerWithIdentifier("FoodChoise") as! FoodChoiseViewController
        
        self.modalPresentationStyle = .CurrentContext
        self.navigationController?.modalPresentationStyle = .CurrentContext
        self.view.window?.rootViewController?.modalPresentationStyle = .CurrentContext
        self.view.window?.rootViewController?.presentViewController(foodChoiseVc, animated: true, completion: nil)
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
